package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.MachineAccountBean
import com.hinnotek.warehouse.databinding.ActivityMachineAccountBinding
import com.hinnotek.warehouse.databinding.ItemMachineAccountLayoutBinding
import com.hinnotek.warehouse.observer.ObserverList
import com.hinnotek.warehouse.viewmodel.MachineAccountViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_WAREHOUSE_LOSS_REPORT_LIST
import com.lee.common.utils.ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT
import com.lee.common.utils.ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_LIST
import com.lee.common.utils.Skipping
import com.lee.common.vo.PageVO
import dagger.hilt.android.AndroidEntryPoint

@Route(path = ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT)
@AndroidEntryPoint
class MachineAccountActivity : BaseActivity<ActivityMachineAccountBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MachineAccountBean, ItemMachineAccountLayoutBinding>
    private val viewModel by viewModels<MachineAccountViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_machine_account)
        setTitle("台账汇总")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()

    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_machine_account_layout,
            ArrayList(),
            BR.machineAccount
        )
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }

        val list: Array<String> = resources.getStringArray(R.array.testArrays2)
        bindingView.spinnerName.setItems(list.toMutableList())

        adapter.addListener { binding, itemData ->
            binding.root.setOnClickListener {
                Skipping.start(ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_LIST,"applyId",itemData.applyId,MACHINE_CONTENT_LIST,itemData,this@MachineAccountActivity)
            }
        }

        viewModel.machineAccountPage.observe(this, object : ObserverList<MachineAccountBean>() {
            override fun onDataChange(data: PageVO<MachineAccountBean>) {
                adapter.addAll(data.datas, viewModel.isFirst)
                super.onDataChange(data)
            }
        })

        bindingView.search.setOnClickListener {
            viewModel.setParameters(
                bindingView.editTextTextPersonName.text.toString(),
                bindingView.spinnerName.text.toString()
            )
            viewModel.autoRefresh()
        }

    }

    override fun setData() {

    }
}